﻿/*
 * Copyright (c) 2015-2016 Constantijn Evenhuis (blueprints@const.nl)
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
using Blueprints.Editing;
using Blueprints.Editor.Commands;
using System.Collections.Generic;

namespace Blueprints.Editor
{
    /// <summary>
    /// Creates views using an IViewFactory.
    /// </summary>
    /// <typeparam name="ViewType">The type of view to create.</typeparam>
    public sealed class ViewProvider<ViewType>
    {
        /// <summary>
        /// Creates a new ViewProvider.
        /// </summary>
        /// <param name="viewFactory">The view factory to use.</param>
        public ViewProvider(IViewFactory<ViewType> viewFactory)
        {
            ViewFactory = viewFactory;
        }

        /// <summary>
        /// Gets the view factory to use.
        /// </summary>
        public IViewFactory<ViewType> ViewFactory
        {
            get;
            private set;
        }

        /// <summary>
        /// Creates a view for <paramref name="node"/>.
        /// </summary>
        /// <param name="node">The node to create a view for.</param>
        /// <returns>The created view.</returns>
        public ViewType CreateView(EditNode node)
        {
            switch (node.EditType)
            {
                case EditType.Object:
                    return CreateObjectView(node);
                case EditType.Collection:
                    return CreateCollectionView(node);
                case EditType.Boolean:
                    return ViewFactory.CreateBooleanView(node, new ContentNodeCommands(node));
                case EditType.Value:
                    return ViewFactory.CreateValueView(node, new ContentNodeCommands(node));
                case EditType.SingleSelect:
                    return ViewFactory.CreateSingleSelectView(node, new ContentNodeCommands(node));
                case EditType.Link:
                    return ViewFactory.CreateLinkView(node, new LinkNodeCommands(node));
                case EditType.MultiSelect:
                    return ViewFactory.CreateMultiSelectView(node, new ContentNodeCommands(node));
                case EditType.Unknown:
                default:
                    return ViewFactory.CreateUnknownView(node, new UnknownNodeCommands(node));
            }
        }

        private ViewType CreateObjectView(EditNode node)
        {
            List<PropertyView<ViewType>> properties = new List<PropertyView<ViewType>>();
            foreach (EditProperty property in node.Properties.Values)
            {
                IEditorCommands commands;
                ViewType view;
                if (property.Node == null)
                {
                    if (property.EditType == EditType.Collection || property.EditType == EditType.Object)
                    {
                        commands = new UnsetReferencePropertyCommands(property);
                        view = ViewFactory.CreateUnsetPropertyView(property, commands);
                    }
                    else
                    {
                        commands = new UnsetContentPropertyCommands(property);
                        view = ViewFactory.CreateUnsetPropertyView(property, commands);
                    }
                }
                else
                {
                    commands = new SetPropertyCommands(property);
                    view = CreateView(property.Node);
                }

                properties.Add(new PropertyView<ViewType>(property, commands, view));
            }

            return ViewFactory.CreateObjectView(node, new ObjectNodeCommands(node), properties);
        }

        private ViewType CreateCollectionView(EditNode node)
        {
            List<NodeView<ViewType>> elements = new List<NodeView<ViewType>>();
            foreach (EditNode element in node.Elements)
            {
                elements.Add(new NodeView<ViewType>(element, new ElementCommands(node, element), CreateView(element)));
            }

            return ViewFactory.CreateCollectionView(node, new CollectionNodeCommands(node), elements);
        }
    }
}